package gamestates;

import java.io.File;

import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Image;

import com.artemis.World;
import com.artemis.Entity;
import com.artemis.EntitySystem;
import com.artemis.SystemManager;

import components.*;
import systems.*;

public class PlayState extends BasicGameState
{
	private int stateID;
	private World world;
	
	private SystemManager systemManager;
	private EntitySystem movementSystem;
	private EntitySystem inputSystem;
	private EntitySystem renderSystem;
	private EntitySystem thrustSystem;
	
	private static PlayState instance;
	
	public PlayState(int stateID)
	{
		this.stateID = stateID;
	}
	
	public static PlayState getInstance()
	{
		if(instance == null)
		{
			instance = new PlayState(1);
		}
		return instance;
		
	}
	
	@Override
	public void init(GameContainer gc, StateBasedGame sb) throws SlickException
	{
		world = new World();
		systemManager = world.getSystemManager();
		movementSystem = systemManager.setSystem(new MovementSystem());
		inputSystem = systemManager.setSystem(new InputSystem(gc));
		renderSystem = systemManager.setSystem(new RenderingSystem());
		thrustSystem = systemManager.setSystem(new ThrustRenderSystem());
		systemManager.initializeAll();
		
		Entity player = world.createEntity();
		player.addComponent(new Player(0.0005f, 0.2f));
		player.addComponent(new Position(100f, 100f));
		player.addComponent(new Velocity(0,0,0.2f));
		player.addComponent(new Sprite(new Image("Images" + File.separator + "SS Junkyard.png")));
		player.addComponent(new Thruster(new Image("Images" + File.separator + "Junkyard Throttle.png")));
		player.refresh();
	}

	@Override
	public void render(GameContainer gc, StateBasedGame sb, Graphics g) throws SlickException 
	{
		renderSystem.process();
		thrustSystem.process();
	}

	@Override
	public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException 
	{
		world.loopStart();
		inputSystem.process();
		movementSystem.process();
		world.setDelta(delta);
		
	}
	/**
	 * 
	 * @param e - The entity that is firing the debris; This entity must have position and sprite components.
	 * @throws SlickException
	 */
	public void createMedDebris(Entity e) throws SlickException
	{
		Entity debris = world.createEntity();
		Position pos = e.getComponent(Position.class);
		Sprite sprite = e.getComponent(Sprite.class);
		Image image = new Image("Images" + File.separator + "SSJ_MD_1.png");
		debris.addComponent(new Position((float)(pos.getX() + (image.getWidth() + sprite.getImage().getWidth())/2 * Math.cos(Math.toRadians(pos.getTheta()))), 
				(float)(pos.getY() + (image.getWidth() + sprite.getImage().getWidth())/2 * Math.sin(Math.toRadians(pos.getTheta())))));
		debris.addComponent(new Velocity((float)Math.cos(Math.toRadians(pos.getTheta())), (float)Math.sin(Math.toRadians(pos.getTheta())),500));
		debris.addComponent(new Sprite(image));
		debris.refresh();
	}
	
	@Override
	public int getID()
	{
		return stateID;	
	}

}
